<?php

class NHelperImage {


static function  resize($filename,$resize_form="50x",$newfilename='',$driver='gd'){

	$functionname='resize_'.$driver;
   NHelperImage::$functionname($filename,$resize_form,$newfilename);
}
static function resize_im($filename,$resize_form="50x",$newfilename=''){
if (!$newfilename) $newfilename=$filename;
$command="convert -resize '$resize_form' $filename  $newfilename ";
//echo $command."\n";
echo system($command);
}

static function getimagesize($filename){
	return getimagesize($filename);
}

static function resize_gd($filename,$resize_form="50x",$newfilename=''){
if (!$newfilename) $newfilename=$filename;
$aArrExt = split('\.',$filename);
$ext=$aArrExt[count($aArrExt)-1];

$image_details = getimagesize($filename);
$width = $image_details[0];
$height = $image_details[1];

//$aGeometry=preg_split('/x/',$resize_form);
$xpos = strpos($resize_form,'x');
if (is_int($xpos) and $xpos>=0){
$geom->x = substr($resize_form,0,$xpos);
$geom->y=substr($resize_form,$xpos+1,strlen($resize_form)-$xpos);
}

if ($geom->x and !$geom->y) {
$res_width=$geom->x;
$percent = $percentage = ($res_width / $width); 
$res_height = $percent*$height;
} elseif ($geom->y and !$geom->x) {
$res_height=$geom->y;

$percent = $percentage = ($res_height / $height); 
$res_width = $percent*$width;
} elseif ($geom->x and $geom->y){
$res_width=$geom->x;
$res_height=$geom->y;
} else { die('Не верный формат');}

//echo $res_width.'x'.$res_height;

$dest = imagecreatetruecolor($res_width, $res_height);

//echo $ext;
 switch ($image_details['mime']){
	case'image/jpeg':
	$source = imagecreatefromjpeg($filename);
	imagecopyresampled ($dest, $source, 0, 0, 0, 0, $res_width, $res_height, $width, $height);
	imagejpeg($dest,$newfilename , 90);
	break;
	case'image/gif':
	$source = imagecreatefromgif($filename);
	imagecopyresampled ($dest, $source, 0, 0, 0, 0, $res_width, $res_height, $width, $height);
	imagejpeg($dest,$newfilename , 90);
	break;
	case'image/png':
	;
	$source = imagecreatefrompng($filename);
	imagecopyresampled ($dest, $source, 0, 0, 0, 0, $res_width, $res_height, $width, $height);
	imagepng($dest,$newfilename);
	break;

	
	
}






imagedestroy($dest);
imagedestroy($source);


}



}
?>